

#include "connection.h"
#include "pfdataparser.h"


#include <QtNetwork>


Connection::Connection(QObject *parent)
    : QTcpSocket(parent)
{
    QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
    state = WaitSize;
    
}


void Connection::procReg(const QByteArray data, bool& isRegMsg)
{
    isRegMsg = false;
    QString type = "";
    QString strhead = data.left(10);
    QString strid = data.mid(10,2);
    int id = strid.toInt();
    if ( strhead == "SIM_REG_ZC")
    {
        isRegMsg = true;
        type = "zc";
        qDebug() << "It is ZC registe.";
    }
    else if ( strhead == "REG_SIM_CC")
    {
        isRegMsg = true;
        type = "cc";
        const char *p = "";
        int r = write(p);
        qDebug() << "It is ATP registe." <<r;
    }
    else if ( strhead == "REG_SIM_TO")
    {
        isRegMsg = true;
        type = "ato";
        write("");
        qDebug() << "It is ATO registe.";
    }
    if(isRegMsg)
     emit clientReg(type, socketDescriptor(), id);
}


void Connection::processReadyRead()
{
    int bytesInSock = bytesAvailable() ;
    static int bytesRemainder = 0;
    static int dataR = 0;
   
    while( bytesAvailable() >= 16)
    {
        if(state == WaitSize)
        {
            bytes = read(16);
            int totalsize = 0;
       
            memcpy(&totalsize, bytes.data()+12, sizeof(int));
            totalsize = utility::ByteSwap32(totalsize);

            QString strhead = bytes.left(3);
            if ( strhead != "SIM"
                 && strhead != "REG"
                 )
            {
                qDebug() << "This is not data type."
                        << "It only support SIM and REG. Now is"
                        << strhead;
                return;
            }

            bool isRegMsg = false;
            procReg(bytes, isRegMsg);
            if (isRegMsg) 
            {
                emit dataReady(bytes, 0x90000001);
                return;
            }
            state = WaitData;
  
            dataR = dataSize = totalsize - 16;
            bytesInSock = bytesAvailable() ;//test
            continue;
        }
        else
        {
            int lent = 0 ;
            while( dataR > 0)
            {
                lent = bytesAvailable() ;
                QByteArray dataArray = read(lent);
                bytes.append(dataArray);
                dataR = dataR - lent; 
                if(dataR > 0)
                    return;
            }

            {
                QString strhead =  bytes.left(16);
                QString strid = strhead.mid(6,6);
                int id = strid.toInt();
                if (strhead.startsWith("SIM_ZC"))
                {
                    id = 0x20000000;
                }
                else if (strhead.startsWith("SIM_TO"))
                {
                    id = 0x40000000 + id;
                }
                else if (strhead.startsWith("SIM_CC"))
                {
                    qDebug() << "It is CC data.";
                    id = 0x80000000 + id;
                }
                
                emit dataReady(bytes.mid(16), id);
                state = WaitSize;
                bytesInSock = bytesAvailable() ;//test
                continue;
            }
        }
    }
       
}

